﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace MO.BusinessServer
{
    public class UserManager
    {
        public static bool LoginCheck(string userCode, string password, out string message, out object context)
        {
            context = null;
            message = null;
            if (!string.IsNullOrEmpty(userCode))
            {
                DataTable table = DbOperation.DB.ExecuteDataTable("select * from USER where USER_CODE={0}", userCode);
                if (table.Rows.Count == 0)
                {
                    message = "用户名不存在";
                    return false;
                }
                DataRow row = table.Rows[0];
                string pass = row["USER_PASSWORD"] as string;
                if (pass == MD5.MD5Hash(password))
                {
                    context = new Context()
                    {
                        User = new UserDefine()
                        {
                            Code = userCode,
                            Name = row["USER_NAME"] as string,
                            OrganCode = row["USER_ORGAN"] as string,
                            RoleCode = row["USER_ROLE"] as string
                        }
                    };
                    return true;
                }
                else
                    message = "密码错误";
            }
            else
            {
                message = "用户名不能为空";
            }
            return false;
        }
    }
}
